package com.lzy.dao.im;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.lzy.dao.HibernateDao;
import com.lzy.entity.dto.IMBean;
import com.lzy.util.Page;
import com.lzy.util.PageRequest;

@Component
public class ImChatLogImpl extends HibernateDao implements ImChatLogManager{

	private Logger log = LoggerFactory.getLogger(ImChatLogImpl.class);
	
	public Page queryListBySql(String sql,Map<String, Object> params,PageRequest pageRequest) {
		
		Page page = new Page(pageRequest);
		
		Session session = sessionFactory.getCurrentSession();
		SQLQuery query = session.createSQLQuery(sql);
//		query.setFirstResult(page.getStart()) ;
//		query.setMaxResults(page.getLimit());
		query.setProperties(params);
		query.setResultTransformer(Transformers.aliasToBean(IMBean.class));
		log.info("查询记录开始");
		List list = query.list();
		log.info("查询记录结束");
		
		page.setTotalItems( getCount(sql,params) );
		
		page.setResult(list);
		
		return page;
	}
	
	public Page findPage(String whereCondition, Map<String, Object> params,
			PageRequest pageRequest) {
		log.debug("findPage start");
		Page page = new Page(pageRequest);
		Session session = sessionFactory.getCurrentSession();
		String orderby = " order by CaptureTime desc" ;
		SQLQuery query = session.createSQLQuery("select * from tbim " + whereCondition + orderby);
//		query.setFirstResult(page.getStart()) ;
//		query.setMaxResults(page.getLimit());
		query.setResultTransformer(Transformers.aliasToBean(IMBean.class));
		List list = query.list();
		page.setResult(list);
		SQLQuery countQuery = session.createSQLQuery("select count(rownum) from tbim "+whereCondition);
		int count= ((BigDecimal) countQuery.uniqueResult()).intValue();
		
		page.setTotalItems( count );
		log.debug("findPage end");
		return page;
	}
	
	public long getCount(String sql,Map<String, Object> params) {
		log.info("查询总数开始");
		Session session = sessionFactory.getCurrentSession();
		Object count =session.createSQLQuery("select count(rownum) from ( " + sql +") ")
				.setProperties(params)
				.uniqueResult();
		log.info("查询总数结束");
//		Object count =session.createSQLQuery( sql )
//				.setProperties(params)
//				.setMaxResults(5000).list().size();
//		return (Integer)count;
		return ((BigDecimal)count).intValue();
	}
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		SessionFactory factory = (SessionFactory) applicationContext.getBean("sessionFactory");
		Session session = factory.openSession();
		
		Query query = session.createSQLQuery("select * from TBIM where content like ?");
		query.setString(0, "nickname");
		Object object = query.list().size();
		System.out.println(object);
	}
}
